﻿using mozhi.smarterp.Finance.Basedatas.Repository;
using mozhi.smarterp.Finance.Enums;
using System.Threading.Tasks;
using Volo.Abp;

namespace mozhi.smarterp.Finance.Basedatas.Services
{
    public class TagManager : DomainServiceBase
    {
        public TagManager(IDataAssociationRepository dataAssociationRepository) : base(dataAssociationRepository)
        {
            _dataAssociationRepository = dataAssociationRepository;
        }

        public async Task ChangeType(Tag tag , TagType type)
        {
            if (type == tag.Type)
            {
                tag.ChangeType(type);
                return;
            }
               

            //如果引用不能修改标签类型
            var dataAssociations = await GetDataAssociationDetail(tag.Id);

            if(dataAssociations.Count>0)
                throw new UserFriendlyException($"存在数据引用:{string.Join(";", dataAssociations)},不能更改标签类型", "100000");

            tag.ChangeType(type);

        }


        public async Task RemoveItem(Tag tag , TagItem item)
        {
            var dataAssociations = await GetDataAssociationDetail(item.Id);

            if (dataAssociations.Count > 0)
                throw new UserFriendlyException($"存在数据引用:{string.Join(";", dataAssociations)},不能删除标签项目", "100000");

            tag.Items.Remove(item);
        }
    }
}
